package com.futvan.z.erp.customer;

import com.futvan.z.framework.common.bean.Code;
import com.futvan.z.framework.common.bean.Result;
import com.futvan.z.framework.common.service.CommonService;
import com.futvan.z.framework.core.SuperAction;
import com.futvan.z.framework.core.z;
import com.futvan.z.framework.util.StringUtil;
import com.futvan.z.framework.util.enums.ZxUtil;
import com.futvan.z.shop.shop.s_shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Random;

@Controller
public class CustomerAction extends SuperAction{
	@Autowired
	private CustomerService customerService;

	@Autowired
	private CommonService commonService;


	/**
	 * 打开客户展期页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/open_customer_renew")
	public ModelAndView open_recharge() throws Exception {
		ModelAndView model = new ModelAndView("erp/customer/customer_renew");
		if(isMobile()) {
			model.addObject("is_mobile", "1");
		}
		return model;
	}

	/**
	 * 打开退款页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/ReplaceCustomerMainNumber")
	public ModelAndView ReplaceCustomerMainNumber() throws Exception {
		ModelAndView model = new ModelAndView("erp/customer/ReplaceCustomerMainNumber");
		if(isMobile()) {
			model.addObject("is_mobile", "1");
		}
		return model;
	}

	/**
	 * 打开导入直销会员
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/ImportZXCustomer")
	public ModelAndView ImportZXCustomer() throws Exception {
		ModelAndView model = new ModelAndView("erp/customer/ImportZXCustomer");
		if(isMobile()) {
			model.addObject("is_mobile", "1");
		}
		return model;
	}


	/**
	 * 打开客户通知发送工具
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/sendCustomerNotices")
	public ModelAndView sendCustomerNotices() throws Exception {
		ModelAndView model = new ModelAndView("erp/customer/sendCustomerNotices");
		if(isMobile()) {
			model.addObject("is_mobile", "1");
		}
		return model;
	}

	/**
	 * 打开客户通知短信发送工具
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/sendCustomerMessage")
	public ModelAndView sendCustomerMessage() throws Exception {
		ModelAndView model = new ModelAndView("erp/customer/sendCustomerNoticesMessage");
		if(isMobile()) {
			model.addObject("is_mobile", "1");
		}
		return model;
	}


	
	@RequestMapping(value="/customerLogin")
	public @ResponseBody Result Service(@RequestParam HashMap<String, String> bean) {
		Result result = new Result();
 		Result authority = z.isServiceAuthority(bean,request);
 		if(Code.SUCCESS.equals(authority.getCode())){
 			String username = bean.get("username");
 			String password = bean.get("password");
 			if(z.isNotNull(username)){
 				if(z.isNotNull(password)){
 					crm_customer cq = new crm_customer();
 					cq.setUsername(username);
 					cq.setIs_enabled("1");
 	 				crm_customer c = sqlSession.selectOne("crm_customer_select", cq);
 	 				String jiami_password = StringUtil.CreatePassword(password);
 	 				if(jiami_password.equals(c.getPassword_login())) {
 	 					result.setCode(Code.SUCCESS);
 	 					result.setData(c);
 	 				}else {
 	 					result.setCode(Code.ERROR);
 	 	 				result.setMsg("密码错误");
 	 				}
 	 			}else {
 	 				result.setCode(Code.ERROR);
 	 				result.setMsg("password is null");
 	 			}
 			}else {
 				result.setCode(Code.ERROR);
 				result.setMsg("username is null");
 			}
 		}else {
 			result = authority;
 		}
		return result;
	}



	@RequestMapping(value="/SendCustomerToZX")
	public @ResponseBody Result SendCustomerToZX(@RequestParam HashMap<String, String> bean) {
		Result result = new Result();
		Result authority = z.isServiceAuthority(bean,request);
		if(Code.SUCCESS.equals(authority.getCode())){
			String type = bean.get("type");
			String zids = bean.get("zids");
			String sqlzids = StringUtil.ListToSqlInArray(zids);
			String sql = "select * from crm_customer where zid in ("+sqlzids+")";
			List<crm_customer> c = sqlSession.selectList("crm_customer_select_sql", sql);
			for (crm_customer customer : c) {
				ZxUtil.SendCustomerToZX(type, customer);
			}
			result.setCode(Code.SUCCESS);
			result.setData(c);
		}else {
			result = authority;
		}
		return result;
	}


	/**
	 * 用客户信息同步生成或更新店铺信息
	 * @param bean
	 * @return
	 */
	@RequestMapping(value="/synchronization_shop")
	public @ResponseBody Result synchronization_shop(@RequestParam HashMap<String, String> bean) {
		Result result = new Result();
		String zids = bean.get("zids");
		if(z.isNotNull(zids)){
			String [] zidsarray = zids.split(",");
			for (String zid : zidsarray) {

				crm_customer c = sqlSession.selectOne("crm_customer_select_zid",zid);
				if(z.isNotNull(c)){
					//判读是否有店铺信息
					List<s_shop> list = sqlSession.selectList("s_shop_select_sql","select * from s_shop where customid = '"+zid+"'");
					if(z.isNotNull(list) && list.size()>0){
						//执行更新
						sqlSession.update("update","update s_shop set name = '"+c.getName()+"' ,tel='"+c.getTel()+"',welcome='欢迎光临',info='欢迎进入"+c.getName()+"的微店' where customid = '"+zid+"'");
					}else{
						//执行新增
						s_shop s = new s_shop();
						String shopnumber = z.CreateShopNumber();
						s.setZid(shopnumber);
						s.setNumber(shopnumber);
						s.setName(c.getName());
						s.setTel(c.getTel());
						s.setCustomid(c.getZid());
						s.setWelcome("欢迎光临");
						s.setInfo("欢迎进入"+c.getName()+"的微店");
						sqlSession.insert("s_shop_insert",s);
					}
				}





			}
			result.setSuccess();
		}else{
			result.setError("zids is null");
		}
		return result;
	}


	/**
	 * 变更创建时间 1949-01-01 00:00:00
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/update_create_time_1949")
	public @ResponseBody Result update_create_time_1949(@RequestParam HashMap<String,String> bean) throws Exception {
		Result result = new Result();
		try {
			//获取所有客户主键
			String zids = bean.get("zids");
			String[]zid_array = zids.split(",");
			//遍历所有客户主键
			for (int i = 0; i < zid_array.length; i++) {
				String zid = zid_array[i];
				if(z.isNotNull(zid.trim())){
					String sql = "update crm_customer set create_time = '1949-01-01 00:00:00' where zid = '"+zid+"'";
					//变更创建时间
					sqlSession.update("update", sql);
				}
			}
			result.setCode(Code.SUCCESS);
			result.setMsg("变更创建时间成功");
		} catch (Exception e) {
			result.setCode(Code.ERROR);
			result.setMsg("变更创建时间出错|"+e.getMessage());
		}
		return result;
	}

	/**
	 * 请空客户经销商协议文件
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/delete_distributor_agreement")
	public @ResponseBody Result delete_distributor_agreement(@RequestParam HashMap<String,String> bean) throws Exception {
		Result result = new Result();
		try {
			//获取所有客户主键
			String zids = bean.get("zids");
			//转成sql in 条件
			String zids_sql_in = StringUtil.ListToSqlInArray(zids);
			//执行SQL
			String sql = "update crm_customer set sign_info = '',sign_img = '',distributor_agreement_signing_date = NULL, distributor_agreement = '',distributor_agreement_2 = '',distributor_agreement_pdf = '',distributor_agreement_pdf_2 = '' where zid in( "+zids_sql_in+")";
			//变更创建时间
			int i = sqlSession.update("update", sql);
			result.setSuccess("成功清空"+i+"名客户经销商协议文件",null);
		} catch (Exception e) {
			result.setCode(Code.ERROR);
			result.setMsg("变更创建时间出错|"+e.getMessage());
		}
		return result;
	}


	/**
	 * 打开设置客户信息审核不合格理由页面
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/OpenSetIsValidityLicence0")
	public ModelAndView OpenSetIsValidityLicence0(@RequestParam HashMap<String,String> bean) throws Exception {
		ModelAndView model = new ModelAndView("erp/customer/SetIsValidityLicence0");
		model.addObject("customid", bean.get("customid"));
		return model;
	}


	/**
	 * 证照信息审核不能过
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/SetIsValidity0")
	public @ResponseBody Result SetIsValidity0(@RequestParam HashMap<String,String> bean) throws Exception {
		Result result = new Result();
		String customid = bean.get("customid");
		String notice_type = bean.get("notice_type");
		String title = bean.get("title");
		String notice = bean.get("notice");
		if(z.isNotNull(customid)){
			if(z.isNotNull(notice_type)){
				if(z.isNotNull(title)){
					if(z.isNotNull(notice)){
						//变更客户审核结果
						int i = sqlSession.update("update","update crm_customer set is_validity_licence = '0' where zid = '"+customid+"'");
						if(i==1){
							//发送审核信息到客户通知表
							bean.put("tableId","crm_customer_notice");
							bean.put("is_tips","1");
							bean.put("info","<p><span style=\"font-size: 24px;\">"+notice+"</span></p>");
							Result tempResult = commonService.insert(bean,request);
							if(tempResult.ifSuccess()){
								result.setSuccess();
							}else{
								z.Exception("证照信息审核不能过异常|发送通知异常|"+tempResult.getMsg());
							}
						}else{
							result.setError("证照信息审核不能过异常|更新客户审核状态返回异常："+i);
						}
					}else{
						result.setError("通信信息不能为空");
					}
				}else{
					result.setError("通信标题不能为空");
				}
			}else{
				result.setError("通知类型不能为空");
			}
		}else{
			result.setError("客户编号不能为空");
		}
		return result;
	}



}
